<!doctype html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title>wcdma配置</title>
    <link href="/static/jquery-ui.css" rel="stylesheet">
    <link href="/static/style.css" rel="stylesheet">
</head>
<body style="margin: 0 auto;">
<div style="margin: 0 8%;">
    <fieldset>
        <legend>设备配置信息</legend>
        <table class="table" border="1">
            <tr>
                <th>bandinfo</th>
                <td>
                    <input type="text" value="" id="bandinfo" class="input" disabled>
                </td>
                <th>band</th>
                <td>
                    <input type="text" value="" id="band" class="input" disabled>
                </td>
            </tr>

            <tr>
                <th>power_status</th>
                <td colspan="3">
                    <textarea style="width: 85%;height: 100px" id="power_status"
                              disabled></textarea>
                </td>
            </tr>
            <tr>
                <th>earfcn</th>
                <td>
                    <input type="text" value="" id="earfcn" class="input">
                </td>
                <th>lac</th>
                <td>
                    <input type="text" value="" id="lac" class="input">
                </td>
            </tr>
            <tr>
                <th>psc</th>
                <td>
                    <input type="text" value="" id="psc" class="input">
                </td>
                <th>cellid</th>
                <td>
                    <input type="text" value="" id="cellid" class="input">
                </td>
            </tr>
            <tr>
                <th>plmnid</th>
                <td colspan="3">
                    <input type="text" value="" id="plmnid" class="input">
                </td>
            </tr>
            <tr>
                <td colspan="4" align="center" style="line-height: 60px">
                    <input type="button" value="保存" class="ui-button" onclick="saveDeviceInfo()"/>
                </td>
            </tr>
        </table>
    </fieldset>


    <br/>


    <fieldset>
        <legend>功放</legend>
        <table style="width: 30%">
            <tr>
                <th width="150px">功放</th>
                <td>
                    <select style="width: 150px" id="power">
                        <option value="">--</option>
                        <option value="01">01</option>
                        <option value="02">02</option>
                        <option value="03">03</option>
                        <option value="04">04</option>
                        <option value="05">05</option>
                    </select>
                </td>
                <td>
                    <input type="button" value="保存" class="ui-button" onclick="setPower()"/>
                </td>
            </tr>
        </table>
    </fieldset>

    <br/>

    <fieldset>
        <legend>设备黑白名单</legend>
        <table style="width: 600px">
            <tr>
                <th width="150px">工作模式</th>
                <td colspan="2">
                    <label for="listmode_white"><input type="radio" id="listmode_white"
                                                       name="listmode" value="white">white</label>
                    <label
                            for="listmode_black"><input type="radio" id="listmode_black"
                                                        name="listmode" value="black">black</label>
                </td>
            </tr>
            <tr>
                <th>
                    imsi列表
                </th>
                <td>
                    <div id="imsi_list_div">

                    </div>
                </td>
                <td>
                    <input type="button" value="删除" class="ui-button" onclick="delWhitelist()"/>
                </td>
                </td>
            </tr>
            <tr>
                <th>
                    新增
                </th>
                <td>
                    <input type="text" style="width: 300px" placeholder="请输入要新增的imsi" id="add_imsi"
                           class="input">
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="text" style="width: 300px" placeholder="voice phone"
                           id="add_voice_phone"
                           class="input">
                </td>
                <td>
                    <input type="button" value="新增" class="ui-button" onclick="addWhitelist()"/>
                </td>
                </td>
            </tr>
        </table>
    </fieldset>


    <br/>
    <fieldset>
        <legend>设备操作</legend>
        <table width="30%" style="margin: 30px 100px;">
            <tr>
                <td>
                    <input type="button" value="start" id="start_btn" disabled
                           class="ui-button gray"
                           onclick="setbts('start')"/>
                <td>
                    <input type="button" value="stop" id="stop_btn" disabled class="ui-button gray"
                           onclick="setbts('stop')"/>
                </td>
                <td>
                    <input type="button" value="restart" class="ui-button"
                           onclick="setbts('restart')"/>
                </td>
            </tr>
        </table>

    </fieldset>
</div>
<script src="/static/external/jquery/jquery.js"></script>
<script src="/static/jquery-ui.js"></script>
<script src="/static/common.js"></script>
<script type="text/javascript">

    function addWhitelist() {
        if (!window.confirm("确定要添加吗？")) {
            return;
        }
        var imsi = $.trim($("#add_imsi").val());
        if (imsi.length == 0) {
            alert('请输入要添加的imsi');
            $("#add_imsi").focus();
            return;
        }
        if (imsi.length != 15) {
            alert('imsi格式错误');
            $("#add_imsi").focus();
            return;
        }
        var voicePhone = $.trim($("#add_voice_phone").val());
        if (voicePhone.length == 0) {
            alert('请输入要添加的voice phone');
            $("#add_voice_phone").focus();
            return;
        }
        $.post('/set_list', {
            'operation': 'add',
            'voice_phone': voicePhone,
            'listinfo': imsi
        }, function (response) {
            if (response.result == 'success') {
                queryWhiteList();
                alert('添加成功');
            } else {
                alert('添加失败:' + response.msg);
            }
        });

    }

    function delWhitelist() {
        if (!window.confirm("确定要删除吗？")) {
            return;
        }
        var checkList = $("input[name=imsi_list]:checked");
        if (checkList.length == 0) {
            alert('请选择要删除的imsi');
            return;
        }
        var listinfo = [];
        checkList.each(function () {
            listinfo.push($(this).val());
        });
        if (listinfo.length > 1) {
            alert('每次只能删除一个');
            return;
        }
        $.post('/set_list', {
            'operation': 'del',
            'listinfo': listinfo.join(',')
        }, function (response) {
            if (response.result == 'success') {
                queryWhiteList();
                alert('删除成功');
            } else {
                alert('删除失败:' + response.msg);
            }
        });

    }

    function setbts(oper) {
        var humanTitle = {'start': '启动', 'stop': '关闭', 'restart': '重启'};
        var human = humanTitle[oper];
        if (!window.confirm("确定要" + human + "吗？")) {
            return;
        }
        $.post('/set_deivce_bts', {'setbts': oper}, function (response) {
            if (response.result == 'success') {
                queryBts();
                alert('设备操作执行成功');
            } else {
                alert('设备操作执行失败' + response.msg);
            }
        });
    }

    function saveDeviceInfo() {
        if (!window.confirm("确定要保存吗？")) {
            return;
        }
        var earfcn = $.trim($("#earfcn").val());
        var lac = $.trim($("#lac").val());
        var psc = $.trim($("#psc").val());
        var cellid = $.trim($("#cellid").val());
        var plmnid = $.trim($("#plmnid").val());
        if (earfcn.length == 0) {
            alert('earfcn不能为空');
            return false;
        }
        if (lac.length == 0) {
            alert('lac不能为空');
            return false;
        }
        if (psc.length == 0) {
            alert('psc不能为空');
            return false;
        }
        if (parseInt(psc) > 511) {
            alert('psc最大值不能超过512');
            return false;
        }
        if (cellid.length == 0) {
            alert('cellid不能为空');
            return false;
        }
        if (plmnid.length == 0) {
            alert('plmnid不能为空');
            return false;
        }
        var postData = {
            'earfcn': earfcn,
            'lac': lac,
            'psc': psc,
            'cellid': cellid,
            'plmnid': plmnid
        };
        $.post('/set_device_info', postData, function (response) {
            if (response.result == 'success') {
                queryDeviceInfo();
                alert('设备信息保存成功');
            } else {
                alert('设备信息保存失败：' + response.msg);
            }
        });
    }

    function setPower() {
        var power = $("#power").val();
        if (power === '') {
            alert('请选择功放大小');
            return false;
        }
        $.post('/set_power', {'power': power}, function () {
            if (response.result == 'success') {
                alert('功放设置成功');
            } else {
                alert('功放设置失败');
            }
        });
    }

    function queryDeviceInfo() {
        $.post('/get_device_info', {}, function (response) {
            if (response.status == 200) {
                var data = response.data;
                $("#bandinfo").val(data.bandinfo);
                $("#band").val(data.band);
                $("#earfcn").val(data.earfcn);
                $("#lac").val(data.lac);
                $("#psc").val(data.psc);
                $("#plmnid").val(data.plmnid);
                $("#cellid").val(data.cellid);
                if (data.power_status == -1) {
                    alert('基站未连接');
                    return false;
                }
                var powerStatusList = data.power_status.split(',');
                for (var i = 0; i < powerStatusList.length; i++) {
                    var pMsg = powerStatusList[i];
                    if (pMsg.indexOf('下行功率') != -1) {
                        var power = pMsg.split('：')[1];
                        if (power == 'timeout') {
                            power = '';
                        }
                        $("#power").val(power);
                    }
                }
                $("#power_status").val(powerStatusList.join('\n'));
            } else {
                alert('设备信息查询失败：' + response.msg);
            }
        });
    }


    function queryWhiteList() {
        $.post('/get_white_list', {}, function (response) {
            if (response.status == 200) {
                var listmode = response.data.listmode;
                $("#listmode_" + listmode).prop('checked', true);
                var listinfo = response.data.listinfo;
                var html = '';
                for (var i = 0; i < listinfo.length; i++) {
                    var listItem = listinfo[i];
                    var imsi = listItem.split(' ')[0];
                    html += '<div class="list_div"><input type="checkbox" name="imsi_list" value="' + imsi + '">' + listItem + '</div>';
                }
                $("#imsi_list_div").html(html);
            } else {
                alert('设备黑白名单\n查询失败：' + response.msg);
            }
        });
    }


    function queryBts() {
        $.post('/get_device_bts', {}, function (response) {
            if (response.status == 200) {
                if (response.data == '0') {
                    $("#start_btn").prop('disabled', false);
                    $("#stop_btn").prop('disabled', true);
                    if ($("#start_btn").hasClass('gray')) {
                        $("#start_btn").removeClass('gray');
                    }
                    if (!$("#stop_btn").hasClass('gray')) {
                        $("#stop_btn").addClass('gray');
                    }
                } else {
                    $("#start_btn").prop('disabled', true);
                    $("#stop_btn").prop('disabled', false);
                    if ($("#stop_btn").hasClass('gray')) {
                        $("#stop_btn").removeClass('gray');
                    }
                    if (!$("#start_btn").hasClass('gray')) {
                        $("#start_btn").addClass('gray');
                    }
                }
            }
        });
    }


    $("document").ready(function () {
        queryDeviceInfo();
        queryWhiteList();
        queryBts();
    });


</script>
</body>
</html>
